/*
	create by zhaolei 2011-6-20 version 1.0
	company www.ingit.com
*/
var HH=23;
var MM=59;
var SS=59;
var mode={};
var LOOP;
mode["timer1mode"] = "HH";
mode["timer3mode"] = "HH";
function loopInit(id,sign){
	LOOP=true;				
	change(id,sign);
}
function loopBreak(){
	LOOP=false;
}
function getTag(id,tagName,str,attr){
	var tagArray=document.getElementById(id).getElementsByTagName(tagName);
	for(i=0;i<tagArray.length;i++){
		if(eval("tagArray[i]."+attr)==str){
				return tagArray[i];
		}		
	}	
}
function setMode(id,hms){
	var selectInputPre = getTag(id,'input',mode[id+"mode"],'name');
	var selectInput = getTag(id,'input',hms,'name');
	/*selectInputPre.style.backgroundColor="#FFFFFF";*/
	mode[id+"mode"]=hms;
	/*selectInput.style.backgroundColor="#7FFFD4";*/
}
function change(id,sign){
	if(LOOP){
		var num=getTag(id,'input',mode[id+"mode"],'name').value-0;
		var changeNum=eval(num+sign+1)+"";
		if(changeNum>=0&changeNum<=eval(mode[id+"mode"])){	
			changeNum=complete(changeNum);
			getTag(id,'input',mode[id+"mode"],'name').value=changeNum;		
		}else if(changeNum == eval(mode[id+"mode"]) + 1){
			changeNum=complete(0);
			getTag(id,'input',mode[id+"mode"],'name').value=changeNum;
		}
		else if(changeNum == -1){
			changeNum=eval(mode[id+"mode"]);
			getTag(id,'input',mode[id+"mode"],'name').value=changeNum;
		}
		setTimeout("change('"+id+"','"+sign+"')",200);		
	}		
}
function complete(num){
	while(!(num.length==2)){
		num="0"+num;	
	}
	return num;		
}
function returnTimer(id){
	var strHH=getTag(id,'input','HH','name').value;
	var strMM=getTag(id,'input','MM','name').value;
	var strSS=getTag(id,'input','SS','name').value;
	return strHH+':'+strMM+':'+strSS;
}
function checkNum(id,sign,value){
	if(value<10){
		value=complete(value);
		getTag(id,'input',sign,'name').value=value;
	}else{
		if(value>eval(sign)){
			getTag(id,'input',sign,'name').value=eval(sign);
		}
	}
}
function keyDown(id,key,onFocusObject){
	var nextObject;
	key = (key) ? key : ((window.event) ? window.event : "");  
	var keycode = key.keyCode?key.keyCode:key.which;
	if(onFocusObject.name=='HH'){
		nextObject='MM';
	}else if(onFocusObject.name=='MM'){
		nextObject='SS';
	}else if(onFocusObject.name=='SS'){
		nextObject='HH';
	}
	if(!((keycode>=48 && keycode<=57)||(keycode>=96 && keycode<=105)||(keycode==8)||(keycode==46)||(keycode>=37 && keycode<=40))){
		key.preventDefault();
	}
//	if(key==37||key==39){
//		getTag(id,'input',nextObject,'name').focus();
//	}
	if(keycode==38){
		loopInit(id,'+');
	}
	if(keycode==40){
		loopInit(id,'-');
	}				
}
function keyUp(key){
	key = (key) ? key : ((window.event) ? window.event : "");  
	var keycode = key.keyCode?key.keyCode:key.which;
	if(keycode==38||40){
		loopBreak();
	}		
}
